﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vara_Data_Layer.Vara_Data_Audit
{
    public class Vara_Data_Audit_Controller : IVara_Data_Audit_Actions
    {
        public IVara_Data_Audit InsertAudit(IVara_Data_Audit Vara_Data_Audit)
        {
            int intNewID = 0;
            using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
            {
                SqlEngineProcedureVariable[] var = {
                                                    new SqlEngineProcedureVariable("@AssetID", Vara_Data_Audit.AssetID, SqlDbType.Int),
                                                    new SqlEngineProcedureVariable("@Date",Vara_Data_Audit.Date, System.Data.SqlDbType.DateTime),
                                                    new SqlEngineProcedureVariable("@CompletedBy",Vara_Data_Audit.CompletedBy.ToString()),
                                                    new SqlEngineProcedureVariable("@Outcome",Vara_Data_Audit.Outcome.ToString()),
                                                    new SqlEngineProcedureVariable("@Actions",Vara_Data_Audit.Actions.ToString()),
                                                    new SqlEngineProcedureVariable("@Deleted",Vara_Data_Audit.Deleted, System.Data.SqlDbType.Bit)
                                                 };
                intNewID = int.Parse(NewSqlEngine.ExecuteProcedure("Insert_Audit", var).Rows[0]["ID"].ToString());

                return ReturnAudit(intNewID);
            }

        }

        public IVara_Data_Audit ReturnAudit(int ID)
        {
            DataRow DataRowReturnedAudit;
            IVara_Data_Audit AuditToReturn;
            using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
            {
                DataRowReturnedAudit = NewSqlEngine.ExecuteProcedure("Return_Audit", new SqlEngineProcedureVariable("@ID", ID, System.Data.SqlDbType.Int)).Rows[0];
            }
            AuditToReturn = new Vara_Data_Audit(int.Parse(DataRowReturnedAudit["ID"].ToString()));
            AuditToReturn.AssetID = (int.Parse(DataRowReturnedAudit["AssetID"].ToString()));
            AuditToReturn.Date = DateTime.Parse(DataRowReturnedAudit["Date"].ToString());
            AuditToReturn.CompletedBy = DataRowReturnedAudit["CompletedBy"].ToString();
            AuditToReturn.Outcome = DataRowReturnedAudit["Outcome"].ToString();
            AuditToReturn.Actions = DataRowReturnedAudit["Actions"].ToString();
            AuditToReturn.Deleted = bool.Parse(DataRowReturnedAudit["Deleted"].ToString());

            return AuditToReturn;
        }

        public void UpdateAudit(IVara_Data_Audit Vara_Data_Audit)
        {
            if (Vara_Data_Audit.ID > 0)
            {
                using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
                {
                    SqlEngineProcedureVariable[] var = {
                                                            new SqlEngineProcedureVariable("@ID",Vara_Data_Audit.ID.ToString()),
                                                            new SqlEngineProcedureVariable("@AssetID", Vara_Data_Audit.AssetID, SqlDbType.Int),
                                                            new SqlEngineProcedureVariable("@Date", Vara_Data_Audit.Date, SqlDbType.DateTime),
                                                            new SqlEngineProcedureVariable("@CompletedBy", Vara_Data_Audit.CompletedBy.ToString()),
                                                            new SqlEngineProcedureVariable("@Outcome", Vara_Data_Audit.Outcome.ToString()),
                                                            new SqlEngineProcedureVariable("@Actions", Vara_Data_Audit.Actions.ToString()),
                                                            new SqlEngineProcedureVariable("@Deleted", Vara_Data_Audit.Deleted, SqlDbType.Bit)
                                                       };
                    NewSqlEngine.ExecuteProcedure("Update_Audit", var);
                }
            }
        }

        public void DeleteAudit(IVara_Data_Audit Vara_Data_Audit)
        {
            if (Vara_Data_Audit.ID > 0)
            {
                using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
                {
                    SqlEngineProcedureVariable[] var = {
                                                            new SqlEngineProcedureVariable("@ID",Vara_Data_Audit.ID.ToString()),
                                                            new SqlEngineProcedureVariable("@Deleted", Vara_Data_Audit.Deleted.ToString())
                                                        };
                    NewSqlEngine.ExecuteProcedure("Delete_Audit", var);
                }
            }
        }
        

    }
}
